#!/usr/bin/env ruby

dir = ARGV[0] || ""
unless File.directory?(dir)
  $stdout.puts "ERROR: Missing or invalid directory!"
  exit 1
end

results = {}
Dir["#{dir}/*.txt"].each do |f|
  if File.basename(f) =~ /([^-]+)[-](Prev|Next)\.txt$/
    suite = $1
    ver = $2
    txt = File.read(f)

    txt.scan(/(#{suite}\.\S+).*?thrpt\s+\d+\s+(\d+\.\d+)/).each do |line|
      name = line[0]
      results[suite] ||= {}
      results[suite][name] ||= {}
      results[suite][name][ver] = line[1]
    end
  end
end

max_length = 0
results.keys.each do |suite|
  results[suite].keys.each do |name|
    max_length = name.length if max_length < name.length
  end
end

results.keys.sort.each do |suite|
  results[suite].keys.sort.each do |name|
    puts sprintf(
      "| %-#{max_length+1}s | %11s | %11s |",
      name,
      results[suite][name]["Prev"],
      results[suite][name]["Next"])
  end
end
